package cn.t.repository.impl.order;

import cn.t.converter.order.ExceptionLogConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.enums.OptionTypeEnum;
import cn.t.enums.order.ErrorTypeEnum;
import cn.t.model.order.ExceptionLogDO;
import cn.t.persistence.entity.order.ExceptionLogPO;
import cn.t.persistence.mapper.order.ExceptionLogMapper;
import cn.t.repository.order.ExceptionLogRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备异常日志记录表(ExceptionLog)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:09
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ExceptionLogRepositoryImpl extends RepositoryImpl<ExceptionLogMapper, ExceptionLogPO, ExceptionLogConverter, ExceptionLogDO> implements ExceptionLogRepository {


    @Override
    public List<ExceptionLogDO> findByModel(ExceptionLogDO exceptionLogDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<ExceptionLogPO> list = getWrapper(exceptionLogDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }



    @Override
    public List<ExceptionLogDO> queryListByNo(List<String> orderSns) {
        LambdaQueryChainWrapper<ExceptionLogPO> wrapper = lambdaQuery();
        List<ExceptionLogPO> list = wrapper.in(ExceptionLogPO::getOrderSn, orderSns).ne(ExceptionLogPO::getExceptionType, ErrorTypeEnum.ERROR10.getCode()).list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    @Override
    public Boolean addBatch(List<ExceptionLogDO> exceptionLogDOList) {
      return  this.saveBatch(exceptionLogDOList.stream().map(converter::model2Po).collect(Collectors.toList()));
    }

    @Override
    public Boolean updateBatch(List<ExceptionLogDO> exceptionLogDOList) {
        return  saveOrUpdateBatch(exceptionLogDOList.stream().map(converter::model2Po).collect(Collectors.toList()));
    }
    private LambdaQueryChainWrapper<ExceptionLogPO> getWrapper(ExceptionLogDO exceptionLogDO) {
        LambdaQueryChainWrapper<ExceptionLogPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(exceptionLogDO.getBusinessType())) {
            wrapper.eq(ExceptionLogPO::getBusinessType, exceptionLogDO.getBusinessType());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getOrderSn())) {
            wrapper.eq(ExceptionLogPO::getOrderSn, exceptionLogDO.getOrderSn().trim());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getUserId())) {
            wrapper.eq(ExceptionLogPO::getUserId, exceptionLogDO.getUserId());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getUserName())) {
            wrapper.like(ExceptionLogPO::getUserId, exceptionLogDO.getUserName().trim());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getParticipantId())) {
            wrapper.eq(ExceptionLogPO::getParticipantId, exceptionLogDO.getParticipantId());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getFirmId())) {
            wrapper.eq(ExceptionLogPO::getFirmId, exceptionLogDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getDeviceId())) {
            wrapper.eq(ExceptionLogPO::getDeviceId, exceptionLogDO.getDeviceId());
        }
        if (exceptionLogDO.getBusinessType().equals(OptionTypeEnum.rs_open.getCode())) {
            if (!ObjectUtils.isEmpty(exceptionLogDO.getBusinessType()) &&   !ObjectUtils.isEmpty(exceptionLogDO.getDeviceName())) {
                wrapper.inSql(ExceptionLogPO::getDeviceId,"SELECT device_id FROM t_replenishment  WHERE device_name  'like " +exceptionLogDO.getDeviceName()+"%'");
            }
        }

        if (!ObjectUtils.isEmpty(exceptionLogDO.getCargowayId())) {
            wrapper.eq(ExceptionLogPO::getCargowayId, exceptionLogDO.getCargowayId());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getSkuCode())) {
            wrapper.eq(ExceptionLogPO::getSkuCode, exceptionLogDO.getSkuCode().trim());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getSkuName())) {
            wrapper.like(ExceptionLogPO::getSkuName, exceptionLogDO.getSkuName().trim());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getBeforeWeight())) {
            wrapper.eq(ExceptionLogPO::getBeforeWeight, exceptionLogDO.getBeforeWeight());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getAfterWeight())) {
            wrapper.eq(ExceptionLogPO::getAfterWeight, exceptionLogDO.getAfterWeight());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getExceptionType())) {
            wrapper.eq(ExceptionLogPO::getExceptionType, exceptionLogDO.getExceptionType());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getRemark())) {
            wrapper.eq(ExceptionLogPO::getRemark, exceptionLogDO.getRemark());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getOrderTransferStatus())) {
            wrapper.eq(ExceptionLogPO::getOrderTransferStatus, exceptionLogDO.getOrderTransferStatus());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getGmtModified())) {
            wrapper.between(ExceptionLogPO::getGmtModified, exceptionLogDO.getGmtModifiedStart(), exceptionLogDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(exceptionLogDO.getGmtCreated())) {
            wrapper.between(ExceptionLogPO::getGmtCreated, exceptionLogDO.getGmtCreatedStart(), exceptionLogDO.getGmtCreatedEnd());
        }
        return wrapper;
    }


    @Override
    public long count(ExceptionLogDO exceptionLogDO) {
        return getWrapper(exceptionLogDO).count();
    }
}
